package com.bdqn.struts.action;

import com.bdqn.struts.entity.Login;
import com.bdqn.struts.service.LoginService;
import com.bdqn.struts.service.impl.LoginServiceImpl;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import java.util.Map;

public class LoginAction implements Action {
    LoginService loginService=new LoginServiceImpl();
    private String username;
    private String password;
    Login login=null;

    @Override
    public String execute() throws Exception {
        login = loginService.getLogin(username,password);
        Map<String, Object> session = null;
        session = ActionContext.getContext().getSession();
        if (login != null) {
            if (session.containsKey("CURRENT_USER")) {
                session.remove("CURRENT_USER");
            } else {
               /* session.put("CURRENT_USER",login.getUsername());*/
                session.put("CURRENT_USER", login);
                return "success";
            }
        }
        return "error";
    }

    public LoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Login getLogin() {
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }
}
